/**
 * @author Mariano Albera
 */
var win = Ti.UI.currentWindow;

var tabGroup = win.tabGroup;

win.add(Ti.UI.createLabel({width:50,text:"From:",color:"#999",top:25,left:10}));
var from = Ti.UI.createTextField({value:'lon',height:35,top:20,left:100,width:150,borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED});
win.add(from);

win.add(Ti.UI.createLabel({width:50,text:"To:",color:"#999",top:65,left:10}));
var to = Ti.UI.createTextField({value:'mad',height:35,top:60,left:100,width:150,borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED});
win.add(to);

win.add(Ti.UI.createLabel({width:50,text:"Departing:",color:"#999",top:105,left:10})); 
var dep = Ti.UI.createTextField({value:'any',height:35,top:105,left:100,width:150,editable:false,borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED});
win.add(dep);

dep.addEventListener('focus', function() {
	var popupwin = require("/ui/datePickerPopup");
	popupwin.popup(dep).open();
});


var btn = Ti.UI.createButton({title:'Search'});
win.add(btn);

var toolActInd = Titanium.UI.createActivityIndicator();

var millis = 0;

var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this.responseText holds the raw text return of the message (used for JSON)
        // this.responseXML holds any returned XML (used for SOAP web services)
        // this.responseData holds any returned binary data
        Ti.API.info("Response recieved");
       
        var d = new Date();
		var elapsed  = d.getTime();
       
        Ti.API.info(millis +":"+ elapsed);

        elapsed = elapsed - millis;

        toolActInd.hide();
        win.setToolbar(null,{animated:false});
		Ti.App.fireEvent('gotResults', {resultsText: this.responseText, timeElapsed:elapsed});        

    },
    onerror: function(e) 
    {
        Ti.API.debug(e.error);
        alert('error in ajax: ' + e.error);
    },
    timeout:5000
});

btn.addEventListener('click', function()
{
	toolActInd.style = Titanium.UI.iPhone.ActivityIndicatorStyle.BIG;
	toolActInd.font = {fontFamily:'Helvetica Neue', fontSize:15,fontWeight:'bold'};
	toolActInd.color = 'white';
	toolActInd.message = 'Searching...';
	win.setToolbar([toolActInd],{animated:true});
	//win.setTitleControl(toolActInd);
	toolActInd.show();

	var surl = "http://shopbot.opodo.fr/CacheXMLFeeds/airFeed?SITE_KEY=123&LOW_COST=INCLUDED";
    surl += "&IATA_DEPARTURE=" + from.value.toUpperCase();
    surl += "&IATA_DESTINATION=" + to.value.toUpperCase();
    //surl += "&DEPARTURE_DATE=01-06-2010";
    //surl += "&RETURN_DATE=31-06-2010";
    surl += "&NB_RESULT=99";
    surl += "&ORDER=DATE";
    //surl += "&GROUP_BY=AIRLINE";

	var d = new Date();
	millis = d.getTime();
	
	xhr.open("GET", surl);
	xhr.send();
});

win.addEventListener('focus', function() 
{ 
	Ti.API.info('fired focus event search');
	//from.focus();	

 });


